package com.fingard.dsp.bank.directbank.abc02;

import com.fingard.dsp.bank.directbank.bankInterface.IContract;
import com.fingard.dsp.bank.reqrespobj.contract.*;
import com.fingard.dsp.bank.reqrespobj.contract.detail.RetAtsContractDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

public class ABC02Contract extends ABC02Base implements IContract {
    @Override
    public RetAtsContract contract(ReqContract p_req) throws Exception {
        RetAtsContract retAts = new RetAtsContract(p_req.reqHeader);
        RetAtsContractDetail tmpRetDetail = new RetAtsContractDetail();
        tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;

        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            ABC02Header bankHeader = new ABC02Header();

            if ("01".equals(p_req.contractDetail.step)) {
                bankHeader.CCTransCode = ABC02TransCode.deal_contract_step01;
                bankHeader.ReqSeqNo = p_req.contractDetail.askNumber;
                bankHeader.ReqDateTime = new Date();

                writeCommonHeader(xmlWriter, bankHeader);

                xmlWriter.writeStartElement("Cmp");
                xmlWriter.writeElementString("DbProv", getBankConfig().getMapValueByKey("dbProvCFRTC7", p_req.contractDetail.recAct));
                xmlWriter.writeElementString("DbAccNo", getAbcAccNo(p_req.contractDetail.recAct));
                xmlWriter.writeElementString("DbCur", "01");
                xmlWriter.writeElementClosed("DbLogAccNo");
                xmlWriter.writeElementString("NVoucherType", getBankConfig().getValueAsString("nVoucherTypeCFRTC7"));
                xmlWriter.writeElementString("NFAccNo", getBankConfig().getMapValueByKey("nFAccNoCFRTC7", p_req.contractDetail.recAct));
                xmlWriter.writeEndElement();

                xmlWriter.writeStartElement("Cme");
                xmlWriter.writeElementString("CertType", convertCertTypeToBank(p_req.contractDetail.certType));
                xmlWriter.writeElementString("CertNo", p_req.contractDetail.certNum);
                xmlWriter.writeEndElement();

                xmlWriter.writeStartElement("Corp");
                xmlWriter.writeElementString("CardNo", p_req.contractDetail.payAct);
                xmlWriter.writeElementString("ContPhone", p_req.contractDetail.cellPhone);
                xmlWriter.writeElementString("CardNam", p_req.contractDetail.payName);
            } else if ("02".equals(p_req.contractDetail.step)) {
                bankHeader.CCTransCode = ABC02TransCode.deal_contract_step02;
                bankHeader.ReqSeqNo = p_req.contractDetail.askNumber;
                bankHeader.ReqDateTime = new Date();

                writeCommonHeader(xmlWriter, bankHeader);

                xmlWriter.writeStartElement("Cmp");
                xmlWriter.writeElementString("DbProv", getBankConfig().getMapValueByKey("dbProvCFRTC7", p_req.contractDetail.recAct));
                xmlWriter.writeElementString("DbAccNo", getAbcAccNo(p_req.contractDetail.recAct));
                xmlWriter.writeElementString("DbCur", "01");
                xmlWriter.writeElementClosed("DbLogAccNo");
                xmlWriter.writeElementString("RenewFlag", "");
                xmlWriter.writeElementString("PartAccFlag", "");
                xmlWriter.writeEndElement();

                xmlWriter.writeStartElement("Corp");
                xmlWriter.writeElementString("ContractNo", p_req.contractDetail.authCode);
                xmlWriter.writeElementString("NFAccNo", getBankConfig().getMapValueByKey("nFAccNoCFRTC7", p_req.contractDetail.recAct));
                xmlWriter.writeElementString("CardNo", p_req.contractDetail.payAct);
                String deadLine = p_req.contractDetail.deadLine;
                if (StringHelper.isNullOrEmpty(deadLine)) {
                    deadLine = getBankConfig().getMapValueByKey("contractKeyMapping", "deadLine");
                }
                String singleLimit = p_req.contractDetail.singleLimit;
                if (StringHelper.isNullOrEmpty(singleLimit)) {
                    singleLimit = getBankConfig().getMapValueByKey("contractKeyMapping", "singleLimit");
                }
                if (singleLimit.indexOf(".") == -1) {
                    singleLimit = singleLimit + ".00";
                }
                xmlWriter.writeElementString("StartDate", com.fingard.constant.Format.Date8Format.format(new Date()));
                xmlWriter.writeElementString("EndDate", deadLine);
                xmlWriter.writeEndElement();

                xmlWriter.writeStartElement("Cme");
                xmlWriter.writeElementString("CheckCode", p_req.contractDetail.smsCode);
                String amt = p_req.contractDetail.noteMoney;
                if (amt.indexOf(".") == -1) {
                    amt = amt + ".00";
                }
                xmlWriter.writeElementString("TotalLimit", amt);
                xmlWriter.writeElementString("SingleLimit", singleLimit);
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未能识别的step：" + p_req.contractDetail.step;
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRespCode = tmpBkReader.getFirstText("/ap/RespCode");
                    String tmpRetInfo = tmpBkReader.getFirstText("/ap/RespInfo");
                    String tmpRetRxtInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
                    String tmpRespSource = tmpBkReader.getFirstText("/ap/RespSource");
                    if (StringHelper.hasAnyChar(tmpRespCode)) {
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = tmpRetInfo;
                        if (StringHelper.isNullOrEmpty(tmpRetInfo)) {
                            tmpRetDetail.respInfo = tmpRetRxtInfo;
                        }
                        if ("01".equals(p_req.contractDetail.step)) {
                            tmpRetDetail.authCode = tmpBkReader.getFirstText("/ap/Corp/ContractNo");
                            tmpRetDetail.signConfirmType = "2";
                        } else {
                            if ("0".equals(tmpRespSource)) {
                                tmpRetDetail.signState = ContractState.SUCCESS;
                            } else {
                                tmpRetDetail.signState = ContractState.FAIL;
                            }
                            tmpRetDetail.authCode = tmpBkReader.getFirstText("/ap/Corp/ContractNo");
                        }
                        tmpRetDetail.unifiedCode = getBankConfig().getMapValueByKey("contractMapping", tmpRespCode);
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpRetInfo;
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsContract upContract(ReqContract p_req) throws Exception {
        return null;
    }

    @Override
    public RetAtsContract qryContract(ReqContract p_reqQry) throws Exception {
        RetAtsContract retAts = new RetAtsContract(p_reqQry.reqHeader);
        RetAtsContractDetail tmpRetDetail = new RetAtsContractDetail();
        tmpRetDetail.reqSeqID = p_reqQry.contractDetail.askNumber;
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            ABC02Header bankHeader = new ABC02Header();
            bankHeader.CCTransCode = ABC02TransCode.deal_qrycontract;
            bankHeader.ReqSeqNo = getQryTransID();
            bankHeader.ReqDateTime = new Date();

            writeCommonHeader(xmlWriter, bankHeader);

            xmlWriter.writeStartElement("Cmp");
            xmlWriter.writeElementString("DbProv", getBankConfig().getMapValueByKey("dbProvCFRTC7", p_reqQry.contractDetail.recAct));
            xmlWriter.writeElementString("DbAccNo", getAbcAccNo(p_reqQry.contractDetail.recAct));
            xmlWriter.writeElementString("DbCur", "01");
            xmlWriter.writeElementClosed("DbLogAccNo");
            xmlWriter.writeElementClosed("CrProv");
            xmlWriter.writeElementString("CrAccNo", getAbcAccNo(p_reqQry.contractDetail.payAct));
            xmlWriter.writeElementString("CrCur", "01");
            xmlWriter.writeEndElement();
            xmlWriter.writeStartElement("Corp");
            xmlWriter.writeElementString("NVoucherType", getBankConfig().getValueAsString("nVoucherTypeCFRTC7"));
            xmlWriter.writeElementString("NFAccNo", getBankConfig().getMapValueByKey("nFAccNoCFRTC7", p_reqQry.contractDetail.recAct));
            xmlWriter.writeEndElement();
            xmlWriter.writeElementString("qryflag", "1");
            xmlWriter.writeEndElement();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRespCode = tmpBkReader.getFirstText("/ap/RespCode");
                    String tmpRetInfo = tmpBkReader.getFirstText("/ap/RespInfo");
                    String tmpRetRxtInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
                    String tmpRespSource = tmpBkReader.getFirstText("/ap/RespSource");
                    if (StringHelper.hasAnyChar(tmpRespCode)) {
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = tmpRetInfo;
                        if (StringHelper.isNullOrEmpty(tmpRetInfo)) {
                            tmpRetDetail.respInfo = tmpRetRxtInfo;
                        }
                        String ConFlag = tmpBkReader.getFirstText("/ap/Cmp/ConFlag");
                        if ("1".equals(ConFlag)) {
                            tmpRetDetail.signState = ContractState.SUCCESSNOSAVE;
                        } else {
                            tmpRetDetail.signState = ContractState.UNSIGNED;
                        }
                        tmpRetDetail.signConfirmType = "2";
                        tmpRetDetail.unifiedCode = getBankConfig().getMapValueByKey("contractMapping", tmpRespCode);
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpRetInfo;
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsQryBatContract qryBatContract(ReqQryBatContract p_reqQry) throws Exception {
        return null;
    }
}
